/**
 * Copyright (c) 2010, JWebUnit team.
 *
 * This file is part of JWebUnit.
 *
 * JWebUnit is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * JWebUnit is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with JWebUnit.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.sourceforge.jwebunit.fitnesse;

import java.io.File;
import java.io.IOException;

import junit.framework.AssertionFailedError;
import net.sourceforge.jwebunit.junit.WebTester;
import net.sourceforge.jwebunit.util.TestingEngineRegistry;

/**
 * Wrapper of {@link WebTester} to provide a Spanish translation of the API and
 * to catch {@link AssertionFailedError} and return {@code true} (verification
 * failed) or {@code false} (verification failed).
 * 
 * @author Emilio Gutter
 */
public class FitnesseSpanishTester extends WebTester {

	private WebTester wrappedWebTester;

	public FitnesseSpanishTester() {
		this.wrappedWebTester = new WebTester();
	}

	public void configurarTesterConHtmlUnit() {
		wrappedWebTester
				.setTestingEngineKey(TestingEngineRegistry.TESTING_ENGINE_HTMLUNIT);
	}

	public void iniciarEnHostEnPagina(String baseURL, String webPage) {
		wrappedWebTester.setBaseUrl(baseURL);
		wrappedWebTester.beginAt(webPage);
	}

	public void irALaPagina(String webPage) {
		wrappedWebTester.gotoPage(webPage);
	}

	public void irAlFrame(String nombreFrame) {
		wrappedWebTester.gotoFrame(nombreFrame);
	}

	public void pararseEnElFormulario(String nombreForm) {
		wrappedWebTester.setWorkingForm(nombreForm);
	}

	public String getTextoDeElementoConXpath(String xpath) {
		return wrappedWebTester.getElementTextByXPath(xpath);
	}

	public void ingresarElValorEnElCampo(String value, String field) {
		wrappedWebTester.setTextField(field, value);
	}

	public void ingresarEnCampoElArchivoConTipo(String field, String filePath,
			String fileType) {
		wrappedWebTester.setFileField(field, filePath, fileType);
	}

	public void seleccionarLaOpcionDelCombo(String option, String combo) {
		wrappedWebTester.selectOption(combo, option);
	}

	public void enviarElFormularioPresionandoElBoton(String buttonName) {
		wrappedWebTester.submit(buttonName);
	}

	public void enviarElFormulario() {
		wrappedWebTester.submit();
	}

	public void clickLinkConTextoExacto(String texto) {
		wrappedWebTester.clickLinkWithExactText(texto);
	}
	public void clickLinkConTexto(String texto) {
		wrappedWebTester.clickLinkWithText(texto);
	}
	public void clickLinkConId(String id) {
		wrappedWebTester.clickLink(id);
	}

	public String codigoPaginaActual() {
		return wrappedWebTester.getPageSource();
	}

	public boolean estaPresenteElTexto(String text) {
		try {
			wrappedWebTester.assertTextPresent(text);
			return true;
		} catch (AssertionFailedError e) {
			return false;
		}
	}

	public boolean estaPresenteElFormulario(String formName) {
		try {
			wrappedWebTester.assertFormPresent(formName);
			return true;
		} catch (AssertionFailedError e) {
			return false;
		}
	}

	public boolean estaPresenteElElementoConXpath(String xpath) {
		try {
			wrappedWebTester.assertElementPresentByXPath(xpath);
			return true;
		} catch (AssertionFailedError e) {
			return false;
		}
	}

	public File guardarComoArchivoTemporal(String fileName, String extension) {
		File f = null;
		try {
			f = File.createTempFile(fileName, ".tmp");
			wrappedWebTester.saveAs(f);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return f;
	}
	
	public WebTester getWebTester() {
		return wrappedWebTester;
	}
}
